/* 
1、几何位置信息
 */

const style = {
  Angle: 0,
  Color: 11,
  Space: 0,
  SymHeight: 6,
  SymID: 31,
  SymWidth: 6,
};
/**
 *
 * @param {array} position 设置点的经纬度
 * @param {array} attr [{key:"name",value:"武汉",type:"string"}]
 * @param {object} service {name,layerId}
 * @param {string} layer  调用中地接口,加载的地图文档图层
 */
function addPoint({ position, attr, service, layer }) {
  /* 1、几何信息 */
  var gpoint = new Zondy.Object.GPoint(position[0], position[1]); //createPoint();
  //设置当前点要素的几何信息
  var fGeom = new Zondy.Object.FeatureGeometry({
    PntGeom: [gpoint],
  });
  /* 2、图形参数信息 */
  //描述点要素的符号参数信息
  var pointInfo = new Zondy.Object.CPointInfo(style);
  //设置当前点要素的图形参数信息
  var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
    InfoType: 1,
    PntInfo: pointInfo,
  });
  /* 3、属性 */
  var attValue = attr.map((item) => item.value);
  /* 4、feature */
  //创建一个要素
  var feature = new Zondy.Object.Feature({
    fGeom: fGeom,
    GraphicInfo: webGraphicInfo,
    AttValue: attValue,
  });
  //设置要素为点要素
  feature.setFType(1);
  /* 5、设置要素集featureSet */
  var featureSet = new Zondy.Object.FeatureSet();
  featureSet.clear();
  //设置属性结构
  var cAttStruct = new Zondy.Object.CAttStruct({
    FldName: attr.map((item) => item.key),
    FldNumber: attr.length,
    FldType: attr.map((item) => item.type),
  });
  featureSet.AttStruct = cAttStruct;
  //添加要素到要素数据集
  featureSet.addFeature(feature);
  /* 6、调用服务 */
  //创建一个编辑服务类
  var editService = new Zondy.Service.EditDocFeature(
    service.name,
    service.layerId,
    {
      ip: "localhost",
      port: "6163", //访问IGServer的端口号，.net版为6163，Java版为8089
    }
  );
  //执行添加点要素功能
  editService.add(featureSet, onPntSuccess(layer));
}
//添加点要素回调函数
function onPntSuccess(layer) {
  return function (data) {
    if (data.succeed) {
      alert("添加点要素成功！");
      layer.refresh();
    } else {
      alert("添加点要素失败！");
    }
  };
}
